//
//  FourTableViewCell.m
//  开心一刻
//
//  Created by 欧阳政 on 16/4/8.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "FourTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "PictroctModel.h"
@implementation FourTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.imageview11 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 40, 40)];
        self.imageview11.layer.masksToBounds = YES;
        self.imageview11.layer.cornerRadius = 20;
        
        [self.contentView addSubview:_imageview11];
        self.label1 = [[UILabel alloc] initWithFrame:CGRectMake(60, 22.5, KedWidth - 70, 25)];
        [self.contentView addSubview:_label1];
        self.label2 = [[UILabel alloc] initWithFrame:CGRectMake(15, 60, KedWidth - 30, 25)];
        self.label2.font = [UIFont systemFontOfSize:15];
        self.label2.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_label2];
        self.view1 = [[UIView alloc] initWithFrame:CGRectMake(15, 90, KedWidth - 30, 100)];
//        self.view1.backgroundColor = [UIColor redColor];
        self.view1.userInteractionEnabled = YES;
        [self.contentView addSubview:_view1];
//        self.webview1 = [[UIWebView alloc] init];
//        self.webview1.userInteractionEnabled = NO;
//        self.webview1.scalesPageToFit = YES;
//        [self.view1 addSubview:_webview1];
//        self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
//        [self.button1 setTitle:@"JIF" forState:UIControlStateNormal];
//        self.button1.layer.cornerRadius = 30;
//        self.button1.layer.masksToBounds = YES;
//        self.button1.backgroundColor = [UIColor blackColor];
//        [self.button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        self.button1.alpha = 0.5;
//        [self.button1 addTarget:self action:@selector(button1Action:) forControlEvents:UIControlEventTouchUpInside];
//        [self.view1 addSubview:_button1];
        self.imageview22 = [[UIImageView alloc] initWithFrame:CGRectMake(15, 190, 40, 40)];
        self.imageview22.layer.masksToBounds = YES;
        self.imageview22.layer.cornerRadius = 20;
   
        [self.contentView addSubview:_imageview22];
        
        self.label3 = [[UILabel alloc] initWithFrame:CGRectMake(60, 195, KedWidth - 70, 30)];
        [self.contentView addSubview:_label3];
        self.label4 = [[UILabel alloc] initWithFrame:CGRectMake(15, 220, KedWidth - 30, 30)];
        self.label4.font = [UIFont systemFontOfSize:15];
        self.label4.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:_label4];
        
    }
    return self;
}

//- (void)button1Action:(UIButton *)button
//{
//    NSURLRequest * requset = [NSURLRequest requestWithURL:[NSURL URLWithString:self.model22.mp4_url]];
//    [_webview1 loadRequest:requset];
//
//}
- (void)fuzhiModel:(PictroctModel *)model
{
    [_imageview11 sd_setImageWithURL:[NSURL URLWithString:model.useravatar_url]placeholderImage:[UIImage imageNamed:@"001.png"]];
    [_imageview22 sd_setImageWithURL:[NSURL URLWithString:model.avatar_url]];
    _label1.text = model.name;
    _label2.text = model.content;
    _label3.text = model.user_name;
    _label4.text = model.text;
    CGRect bounds1 = [_label2.text boundingRectWithSize:CGSizeMake(KedWidth - 30, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName] context:nil];
    _label2.frame = CGRectMake(15, 60, KedWidth - 30, bounds1.size.height);
    _view1.frame = CGRectMake(15, 60 + bounds1.size.height, KedWidth - 30, (KedWidth - 30)*model.height/model.width);
    _imageview22.frame = CGRectMake(15, 60 + bounds1.size.height + (KedWidth - 30)*model.height/model.width, 40, 40);
    _label3.frame = CGRectMake(60, 60 + bounds1.size.height + (KedWidth - 30)*model.height/model.width, KedWidth - 75, 30);
     CGRect bounds2 = [_label4.text boundingRectWithSize:CGSizeMake(KedWidth - 30, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName] context:nil];
    _label4.frame = CGRectMake(15, 60 + bounds1.size.height + (KedWidth - 30)*model.height/model.width + 40, KedWidth - 30, bounds2.size.height);

//    _webview1.frame = CGRectMake(0, 0 , KedWidth - 30, (KedWidth - 30)*model.height/model.width);
//    _button1.frame = CGRectMake((KedWidth - 30)/2 - 30, (KedWidth - 30)*model.height/model.width/2 - 30, 60, 60);
//    NSURLRequest * requset = [NSURLRequest requestWithURL:[NSURL URLWithString:self.model22.mp4_url]];
//        [_webview1 loadRequest:requset];
    
    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:model.mp4_url]];
    self.player = [AVPlayer playerWithPlayerItem:item];
    AVPlayerLayer *layer1 = [AVPlayerLayer playerLayerWithPlayer:_player];
    layer1.frame = _view1.frame;
    [self.layer insertSublayer:layer1 atIndex:0];
    
    [_player play];
}
+ (CGFloat)cellHeight:(PictroctModel *)model1
{
     CGRect bounds3 = [model1.content boundingRectWithSize:CGSizeMake(KedWidth - 30, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName] context:nil];
     CGRect bounds4 = [model1.text boundingRectWithSize:CGSizeMake(KedWidth - 30, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:15] forKey:NSFontAttributeName] context:nil];
    if (model1.text == nil)
    {
        return bounds3.size.height + 70 + (KedWidth - 30)*model1.height/model1.width;
    }
    else
    {
    return bounds3.size.height + bounds4.size.height + 110 + (KedWidth - 30)*model1.height/model1.width;
    }
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
